part of "../time.dart";

extension TimeOperation on DateTime {
  // 所有大小比较都是先后顺序的比较
  bool operator >(DateTime other) => isAfter(other);
  bool operator >=(DateTime other) => isAfter(other) || this == other;
  bool operator <(DateTime other) => isBefore(other);
  bool operator <=(DateTime other) => isBefore(other) || this == other;
  // 两个时间戳相减，得到一个Duration
  Duration operator -(DateTime other) => difference(other);
  // 加上一段时间，获得一个新的时间戳
  DateTime operator +(Duration duration) => add(duration);

  /// 判断是否为今天，年月日相等即可
  bool get isToday {
    final now = DateTime.now();
    return year == now.year && month == now.month && day == now.day;
  }

  /// 参照"format规则.md"
  String format(String pattern) => pattern
      .formatYear(year)
      .formatMonth(month)
      .formatDay(day)
      .formatWeekday(weekday)
      .formatHour(hour)
      .formatMinute(minute)
      .formatSecond(second);

  /// 淘宝API所需要的格式
  String get formatTaoBao => format("YYYY-MM-DD hh:mm:ss");

  /// 基础的展示所有信息的格式
  String get formatBasic => format("YY年M月D日 hh:mm:ss");

  Date toDate() => Date.fromDateTime(this);
}

extension TimesOperation on Iterable<DateTime> {
  DateTime get earlist => reduce((a, b) => a < b ? a : b); // 总是取更早的
  DateTime get latest => reduce((a, b) => a > b ? a : b); // 总是取更晚的
}

// 将DateTime转换为64位的二进制表示
Uint8List dateTimeToBinary(DateTime dateTime) {
  int microseconds = dateTime.microsecondsSinceEpoch;

  // 创建一个64位（8字节）的缓冲区
  ByteData byteData = ByteData(8);

  // 将64位整数分割成8个字节并存储
  byteData.setInt64(0, microseconds, Endian.big);

  // 返回Uint8List形式的数据
  return byteData.buffer.asUint8List();
}
